//
//  visitors_A1.m
//  K-Music
//
//  Created by CHUN on 16/1/15.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "visitors_A1.h"
#import "vistors_Cell.h"
#import "vistors_Model.h"


@interface visitors_A1 ()<UITableViewDataSource ,UITableViewDelegate>

@property (nonatomic ,strong) UITableView *visitors_A1_TableView;
@property (nonatomic ,strong) NSMutableArray * visitorsDatas;

@end

@implementation visitors_A1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"最近访客";

    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    [self creatUI];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -creatUI-
- (void)creatUI
{
    self.visitors_A1_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.visitors_A1_TableView.delegate = self;
    self.visitors_A1_TableView.dataSource =self;
    self.visitors_A1_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.visitors_A1_TableView.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:self.visitors_A1_TableView];
    
    [self get_chat_group];
}

- (void)get_chat_group
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/visitors?page=0&size=20&sort=createdDate,DESC"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
//        NSLog(@"成功GET --> %@", dic);
        _visitorsDatas = [NSMutableArray array];
        NSArray * datas = dic[@"body"][@"content"];
        for (NSDictionary * visitorDic in datas) {
            NSDictionary * vDic = visitorDic[@"visitor"];
            vistors_Model * vistorDatas = [vistors_Model objectWithKeyValues:vDic];
            [self.visitorsDatas addObject:vistorDatas];
        }
        [self.visitors_A1_TableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
        [MBProgressHUD showErrorWithStatus:@"个人信息获取失败" toView:self.view];
        
    }];
}



#pragma mark -UITableViewDataSource-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.visitorsDatas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    vistors_Cell * vistorsCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (vistorsCell == nil) {
        vistorsCell = [[vistors_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        [vistorsCell creatUI];
    }
    
    vistors_Model * data = [self.visitorsDatas objectAtIndex:indexPath.row];
    NSLog(@"%@",data);
    
    [vistorsCell.icon sd_setImageWithURL:[NSURL URLWithString:data.avatar] placeholderImage:[UIImage imageNamed:@"king_icon1"]];
    
    vistorsCell.icon.layer.cornerRadius= 30;
    vistorsCell.icon.layer.masksToBounds = YES;
    
    vistorsCell.titleName.text = data.nickName;
    vistorsCell.songerLevel.text = [NSString stringWithFormat:@"歌手等级: %@",data.singerLevelDesc];
    vistorsCell.moneyLevel.text = [NSString stringWithFormat:@"财富等级: %@",data.fortuneLevelDesc];
    vistorsCell.songerDetail.text = data.signature;
    
    
    return vistorsCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
